package com.five.dao.impl.backstage;


import com.five.dao.backstage.DeptDao;
import com.five.entity.backstage.DEPT;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DeptDaoImpl
 * @Description TODO
 * @Author yuanshuaiqiang
 * @Date 2020/11/25 17:13
 */
@Repository
public class DeptDaoImpl implements DeptDao {
    //查询所有数据
    @Override
    public List<Map> queryAll(Integer page, Integer limit, DEPT dept) {
        String sql="select tdd.*,co.count from t_dept tdd left join (\n" +
                "select td.name name,count(*) count from t_staff ts join\n" +
                " t_dept td on ts.deptid=td.id GROUP BY ts.deptid\n" +
                ") co on tdd.name=co.name where 1=1 ";
        if (dept.getId()!=null){
            sql+=" and tdd.id="+dept.getId()+" ";
        }
        if (dept.getName()!=null){
            sql+=" and tdd.name like '%"+dept.getName()+"%' ";
        }
        sql+=" order by tdd.id limit ?,? ";

        List<Map> maps = DBUtil.queryMap(sql,(page-1)*limit,limit);
        return maps;
    }
//删除数据
    @Override
    public int del(DEPT dept) {
        String sql="delete from t_dept where id=?";
        int i = DBUtil.manage(sql, dept.getId());
        return i;
    }

    /**
     * 查询数据条数
     * @return
     * @param dept
     */
    @Override
    public int getCount(DEPT dept) {
        String sql="select count(*) a from t_dept where 1=1 ";
        if (dept.getId()!=null){
            sql+=" and id="+dept.getId()+" ";
        }
        if (dept.getName()!=null){
            sql+=" and name like '%"+dept.getName()+"%' ";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        Map map = maps.get(0);
        String s = map.get("a") + "";
        int i = Integer.parseInt(s);
        return i;
    }

    @Override
    public int upd(DEPT dept) {
        String sql="update t_dept set name=? where id=?";
        int i = DBUtil.manage(sql, dept.getName(), dept.getId());
        return i;
    }

    @Override
    public int add(DEPT dept) {
        String sql="insert into t_dept(id,name) values(null,?)";
        int i = DBUtil.manage(sql,dept.getName());
        return i;
    }

    @Override
    public int bad(Integer id) {
        String sql="update t_dept set status=1 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

    @Override
    public int good(Integer id) {
        String sql="update t_dept set status=0 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }
}
